﻿using Castle.Core.Internal;
using HBKY.Shop.Common;
using HBKY.Shop.Common.Helper;
using HBKY.Shop.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Spi;
using System;
using System.Linq;

namespace ZQCY.Appointment.Extensions.Extensions
{
    public static class JobSetup
    {
        public static void AddJobSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            //ITaskJobService 构造需要 SchedulerCenter 在这里提前注入
            services.AddSingleton<IJobFactory, JobFactory>();
            services.AddSingleton<ISchedulerCenter, SchedulerCenter>();

            //任务是否需要开启
            if (!Appsettings.Option("Middleware", "Job", "Enabled").ObjToBool()) return;

            var types = AppDomain.CurrentDomain
                     .Load("HBKY.Shop.Tasks")
                     .GetTypes().FindAll(o => o.BaseType == typeof(JobBase))?.ToList();
            types?.ForEach(item => services.AddTransient(item));
        }
    }
}
